package cn.iocoder.yudao.framework.business.category.dao;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.category.pojo.po.CategoryEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;

/**
 * 分类 Mapper，由子类继承，子类标注 @{@link org.apache.ibatis.annotations.Mapper}
 *
 * @author 山野羡民
 */
//@org.apache.ibatis.annotations.Mapper
public interface CategoryMapper<DO extends CategoryEntity> extends CoreMapper<DO> {

    default List<DO> selectListByStatus(Integer status) {
        QueryWrapper<DO> wrapper = new QueryWrapper<>();
        wrapper.eq("status", status);
        wrapper.orderByAsc("sort");
        wrapper.orderByDesc("id");
        return selectList(wrapper);
    }

    default Long selectCountByParentId(Long id) {
        return selectCount("parent_id", id);
    }

    default DO selectByParentIdAndName(Long parentId, String name) {
        CoreQueryWrapper<DO> wrapper = new CoreQueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        wrapper.eq("name", name);
        wrapper.orderByAsc("sort");
        wrapper.orderByDesc("id");
        wrapper.limitN(1);
        return selectOne(wrapper);
    }

}
